package com.example.mybatis.service.impl;

import cn.hutool.core.collection.CollUtil;

import com.example.mybatis.bean.Details;
import com.example.mybatis.bean.QuestionBan;
import com.example.mybatis.mapper.DetailsMapper;
import com.example.mybatis.mapper.QuestionBanMapper;
import com.example.mybatis.service.QuestionBanService;
import com.example.mybatis.utils.Page;
import com.example.mybatis.utils.ResultUtil;
import com.example.mybatis.utils.validator.DataValidator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class QuestionBanImpl implements QuestionBanService {
    @Resource
    QuestionBanMapper questionBanMapper;
    @Resource
    DetailsMapper detailsMapper;
    @Override
    public ResultUtil update(QuestionBan questionBan) {
        DataValidator.validator(questionBan);
        questionBanMapper.update(questionBan);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil removeById(Integer id) {
        questionBanMapper.removeById(id);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil getById(Integer id,Integer catalogueId) {
        List<Details> detailsList = detailsMapper.selectList("",catalogueId);
        QuestionBan questionBan =null;
        if(id!=null){
            questionBan=  questionBanMapper.selectById(id);
        }
        return ResultUtil.ok()
                .put("questionBan", questionBan)
                .put("detailsList", detailsList);
    }

    @Override
    public ResultUtil insert(QuestionBan questionBan) {
        DataValidator.validator(questionBan);
        questionBanMapper.insert(CollUtil.newArrayList(questionBan)) ;
        return ResultUtil.ok();
    }


    public ResultUtil selectAll(Integer current, Integer size, String matter, Integer catalogueId) {
       // List<QuestionBan> questionBanList = questionBanMapper.selectPage(new Page(current,size),name,catalogueId);

        PageHelper.startPage(current,size);
        List<QuestionBan> questionBanList = questionBanMapper.selectPage1(matter,catalogueId);
        return ResultUtil.ok()
                .put("dataList",questionBanList)
                .put("total",new PageInfo<>(questionBanList).getTotal());
    }

}

   /* public ResultUtil selectList(String matter,String catalogueId) {
            List<QuestionBan> questionBanList = questionBanMapper.selectList(matter,catalogueId);
        return ResultUtil.ok()
                .put("dataList", questionBanList);
    }
*/


